package com.example.liuapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    int itemNum = 100000;
    EmperorDao roomDao;

    TextView tvRoomInsert;
    TextView tvRoomDelete;
    TextView tvRoomUpdate;
    TextView tvRoomQuery;
    TextView tvRoomTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    public void initRoom(Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                roomDao = MyRoomDatabase.getDatabaseInstance(context).getEmperorDao();
            }
        }).start();
    }

    public void initView() {
        tvRoomInsert = findViewById(R.id.tv_room_insert);
        tvRoomDelete = findViewById(R.id.tv_room_delete);
        tvRoomUpdate = findViewById(R.id.tv_room_update);
        tvRoomQuery = findViewById(R.id.tv_room_query);
        tvRoomTime = findViewById(R.id.tv_room_time);
        tvRoomInsert.setOnClickListener(this);
        tvRoomDelete.setOnClickListener(this);
        tvRoomUpdate.setOnClickListener(this);
        tvRoomQuery.setOnClickListener(this);
    }

    public void initData() {
        initRoom(MainActivity.this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_room_insert: {
                roomInsert();
                break;
            }
            case R.id.tv_room_delete: {
                roomDelete();
                break;
            }
            case R.id.tv_room_update: {
                roomUpdate();
                break;
            }
            case R.id.tv_room_query: {
                roomQuery();
                break;
            }
        }
    }


    public void roomInsert() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                long timeStart = System.currentTimeMillis();
                for (int i = 0; i < itemNum; i++) {
                    Emperor caoCao = new Emperor(Utils.getRandomChar(), Utils.getRandomChar(),Utils.getRandomChar(),Utils.getRandomChar(),Utils.getRandomChar());
                    roomDao.insertEmperor(caoCao);
                    int finalI = i;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvRoomTime.setText("插入：" + finalI + "条");
                        }
                    });
                }
                long timeEnd = System.currentTimeMillis();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvRoomTime.setText("完成耗时："+((timeEnd - timeStart) / 1000)+"s");
                    }
                });
            }
        }).start();
    }

    public void roomDelete(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                long timeStart = System.currentTimeMillis();
                try{
                    List<Emperor> emperors = roomDao.queryEmperors();
                    timeStart = System.currentTimeMillis();
                    Log.d("__roomDelete-1","size:"+emperors.size());
                    if (emperors.size() == 0) return;
                    for (int i = 0; i < emperors.size(); i++) {
                        roomDao.deleteEmperor(emperors.get(i));

                        int finalI = i;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvRoomTime.setText("删除：" + finalI + "条");
                            }
                        });
                    }
                }catch (Exception e){
                    Log.d("__roomDelete-e",""+e.getMessage());
                }

                long timeEnd = System.currentTimeMillis();

                long finalTimeStart = timeStart;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvRoomTime.setText("删除耗时："+((timeEnd - finalTimeStart) / 1000)+"s");
                    }
                });
            }
        }).start();
    }

    public void roomUpdate(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                long timeStart = System.currentTimeMillis();
                try{
                    List<Emperor> emperors = roomDao.queryEmperors();
                    timeStart = System.currentTimeMillis();
                    Log.d("__roomUpdate-1","size:"+emperors.size());
                    if (emperors.size() == 0) return;
                    for (int i = 0; i < emperors.size(); i++) {
                        emperors.get(i).age = "54";
                        roomDao.updateEmperor(emperors.get(i));
                        int finalI = i;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvRoomTime.setText("更新：" + finalI + "条");
                            }
                        });
                    }
                    long timeEnd = System.currentTimeMillis();

                    long finalTimeStart = timeStart;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvRoomTime.setText("更新耗时："+((timeEnd - finalTimeStart) / 1000)+"s");
                        }
                    });
                }catch (Exception e){
                    Log.d("__roomUpdate-e",""+e.getMessage());
                }
            }
        }).start();
    }

    public void roomQuery(){
        final int[] result = {0};
        new Thread(new Runnable() {
            @Override
            public void run() {
                long timeStart = System.currentTimeMillis();
                try{
                    List<Emperor> emperors = roomDao.queryEmperors();

                    Emperor lastEmperor = emperors.get(emperors.size()-1);
                    List<Emperor> queryEmperors = roomDao.queryEmperorsByOr(lastEmperor.name,lastEmperor.age,lastEmperor.height,lastEmperor.gender,lastEmperor.weight);
                    result[0] = queryEmperors.size();

                }catch (Exception e){
                    Log.d("__roomQuery-e",""+e.getMessage());
                }
                long timeEnd = System.currentTimeMillis();

                long finalTimeStart = timeStart;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("__roomQuery-2","1");
                        tvRoomTime.setText("更新耗时："+((timeEnd - finalTimeStart) / 1000)+"s" + "查询到"+result+"个");
                    }
                });
            }
        }).start();
    }
}